
package graphics3d;


public class Matrix {
    
    private double[][] cell;
    private int N, M;   //M - кол-во столбцов
                        //N - кол-во строк
    
    public Matrix() {};
    public Matrix(int N, int M) {
        this.N = N;
        this.M = M;
        cell = new double[N][M];
    }
    public Matrix(int M, double[] list) {
        //if (list.length%M != 0) throw ...;
        this.M = M;
        this.N = list.length/M;
        cell = new double[N][M];
        for (int i=0; i<N; i++)
                for (int j=0; j<M; j++)
                        cell[i][j] = list[M*i+j];
        
    }
    public Matrix(Matrix matrix) {
        N = matrix.N;
        M = matrix.M;
        cell = new double[N][M];
	for (int i=0; i<N; i++)
                for (int j=0; j<M; j++)
                        cell[i][j] = matrix.cell[i][j];
    }

    public Matrix multiply(Matrix matrix) {
	//if (M != matrix.N) throw ...;
        Matrix res = new Matrix(N, matrix.M);
        double sum;
		for (int i=0; i<N; i++)
			for (int j=0; j<matrix.M; j++) {
                                sum = 0;
				for(int k=0; k<M; k++)
					sum+=cell[i][k]*matrix.cell[k][j];
                                res.cell[i][j] = sum;
                        }
	return res;
    }

    public void show() {
        if (N == 0 && M == 0) return;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++)
                System.out.print(cell[i][j] + " ");
            System.out.println();
        }
                
    }
 
    public boolean equils(Matrix matrix) {
        if (this == null || matrix == null) return false;
        if (N != matrix.N || M != matrix.M) return false;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
                if (cell[i][j] != matrix.cell[i][j]) return false;
        return true;
    }
    
    public double cell(int i, int j) {
        return cell[i][j];
    }
    
    public int numberOfColumns() {
        return M;
    }
}
